package com.ruoyi.exam.controller;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.exam.dto.QuestionsDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.Questions;
import com.ruoyi.exam.service.IQuestionsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 试题管理Controller
 *
 * @author ruoyi
 * @date 2021-10-12
 */
@RestController
@RequestMapping("/exam/questions")
public class QuestionsController extends BaseController
{
    @Autowired
    private IQuestionsService questionsService;

    /**
     * 查询试题管理列表
     */
    @PreAuthorize("@ss.hasPermi('exam:questions:list')")
    @GetMapping("/list")
    public TableDataInfo list(Questions questions)
    {
        startPage();
        List<Questions> list = questionsService.selectQuestionsList(questions);
        return getDataTable(list);
    }

    /**
     * 导出试题管理列表
     */
    @PreAuthorize("@ss.hasPermi('exam:questions:export')")
    @Log(title = "试题管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Questions questions)
    {
        List<Questions> list = questionsService.selectQuestionsList(questions);
        ExcelUtil<Questions> util = new ExcelUtil<Questions>(Questions.class);
        return util.exportExcel(list, "试题管理数据");
    }

    /**
     * 获取试题管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:questions:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(questionsService.selectQuestionsById(id));
    }

    /**
     * 新增试题管理
     */
    @PreAuthorize("@ss.hasPermi('exam:questions:add')")
    @Log(title = "试题管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Questions questions)
    {
        // 获取登录人ID
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUserId();
        questions.setUserId(userId);
        questions.setDel("0");
        return toAjax(questionsService.insertQuestions(questions));
    }

    /**
     * 批量新增试题管理
     */
    @PreAuthorize("@ss.hasPermi('exam:questions:addall')")
    @Log(title = "试题管理", businessType = BusinessType.INSERT)
    @PostMapping("/addAll")
    public AjaxResult addAll(@RequestBody QuestionsDto questionsDto)
    {
        // 获取登录人ID
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUserId();
        questionsDto.setUserId(userId);
        questionsDto.setDel("0");
        return toAjax(questionsService.insertQuestionsAll(questionsDto));
    }

    /**
     * 修改试题管理
     */
    @PreAuthorize("@ss.hasPermi('exam:questions:edit')")
    @Log(title = "试题管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Questions questions)
    {
        return toAjax(questionsService.updateQuestions(questions));
    }

    /**
     * 删除试题管理
     */
    @PreAuthorize("@ss.hasPermi('exam:questions:remove')")
    @Log(title = "试题管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(questionsService.deleteQuestionsByIds(ids));
    }


    /**
     * 删除试题管理
     */
    @PreAuthorize("@ss.hasPermi('exam:questions:upload')")
    @Log(title = "试题文件上传", businessType = BusinessType.OTHER)
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file)
    {
        return questionsService.questionsSplitContent(file);
    }



}
